package com.zoe.onelink.logging.trace.dubbo;

import brave.Span;
import brave.rpc.RpcClientRequest;
import cn.hutool.core.lang.Assert;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;

import java.util.Map;

public final class DubboClientRequest extends RpcClientRequest implements DubboRequest {
    final Invoker<?> invoker;
    final Invocation invocation;
    final Map<String, String> attachments;

    DubboClientRequest(Invoker<?> invoker, Invocation invocation, Map<String, String> attachments) {
        Assert.notNull(invoker);
        Assert.notNull(invocation);
        this.invoker = invoker;
        this.invocation = invocation;
        this.attachments = attachments;
    }

    @Override
    public Invoker<?> invoker() {
        return invoker;
    }

    @Override
    public Invocation invocation() {
        return invocation;
    }


    @Override
    public Invocation unwrap() {
        return invocation;
    }

    @Override
    public String method() {
        return DubboParser.method(invocation);
    }

    @Override
    public String service() {
        return DubboParser.service(invoker);
    }

    @Override
    public boolean parseRemoteIpAndPort(Span span) {
        return DubboParser.parseRemoteIpAndPort(span);
    }

    @Override
    protected void propagationField(String keyName, String value) {
        attachments.put(keyName, value);
    }
}
